/*
 * User: rant
 * Date: 25/12/2006
 *
 *
 */
package laMalinche.rete.nodes.tailing;

import java.util.*;

/**
 *
 */
public abstract class WorkingMemoryListenable implements WorkingMemoryListenableIfc {

	private List<WorkingMemoryChangeListener> workingMemoryListeners = new ArrayList<WorkingMemoryChangeListener>();

	public List<WorkingMemoryChangeListener> getWorkingMemoryListeners() {
		return workingMemoryListeners;
	}

	public void addWorkingMemoryListener(WorkingMemoryChangeListener listener) {
		workingMemoryListeners.add(listener);
	}

	public void initializeListeners() {
		workingMemoryListeners = new ArrayList<WorkingMemoryChangeListener>();
	}

	public void removeWorkingMemoryListener(WorkingMemoryChangeListener name) {
		getWorkingMemoryListeners().remove(name);
	}

	protected void adviseListeners(final Boolean toValue) {
		List<WorkingMemoryChangeListener> memoryListeners = getWorkingMemoryListeners();
		for (WorkingMemoryChangeListener listener : memoryListeners) {
			WorkingMemoryEvent event = new WorkingMemoryEvent();
			event.setMemory(this);
			event.setConditionMet(toValue);
			listener.workingMemoryChange(event);
		}
	}
}